package Chat;


import java.io.*;
import java.net.*;
import java.util.Vector;

public class ManClient extends Thread{

	private ObjectOutputStream out;
	private ObjectInputStream in;
        private Vector clientesC = new Vector();
        private Socket client;

	public ManClient(Socket client){
		this.client=client;
                clientesC.add(this);

	}

        public Socket getClient(){
            return client;
        }
        

	public void run() {

		try{


                        while(true){
			out = new ObjectOutputStream(client.getOutputStream());
			in = new ObjectInputStream(client.getInputStream());
                        leer();



                    }
		}catch(Exception ex){

		}
	}

	//public void escribir(String linea){
	//	try{
	//		out.writeObject(linea);
	//	}catch(Exception ex){

	//	}
//	}

	public void leer(){
		try{
				String entrada = in.readUTF();
				if(entrada.equals("Bff")){
					String clientesInfo="";
                                        for(int i=0;i<clientesC.size();i++){
                                            ManClient c = (ManClient)clientesC.get(i);
                                           clientesInfo= c.getClient().getInetAddress().getHostAddress()+":"+c.getClient().getPort()+"\r\n";

                                        }
                                        out.writeUTF(clientesInfo);

				//Thread.sleep(15);
                    }
                                else
                                    broadcast(entrada);
			}catch(Exception ex){
                    clientesC.remove(this);


		}
	}

	public void cerrarServidor(){
		try{
                        in.close();
                        out.close();
                        client.close();
		}catch(Exception ex){

		}

	}

        public void broadcast(String mensaje) throws IOException {
            for(int i=0;i<clientesC.size();i++){
                ManClient c = (ManClient)clientesC.get(i);
                c.getOut().writeUTF(mensaje);


            }
        }

         public ObjectOutputStream getOut() {
        return out;
    }

}

